﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Text.RegularExpressions;
using System.IO;
using System.Globalization;

namespace TodesesserMapEditor
{
    public partial class DrawingPanel : Form
    {
        Graphics graphicsObject;
        int mouseX;
        int mouseY;
        Rectangle basicWall;
        SolidBrush basicBrush;
        Boolean loopMovement = false;
        public ArrayList wallArray = new ArrayList();
        public ArrayList objectArray = new ArrayList();
        int selectedWall = 0;
        string selectedType = "null";
        public static int redrawRate = 5;
        public string sizeState = "null";
        public wallObject sizeWall = null;
        public int tempwmousex = 0;
        public int tempwmousey = 0;
        public DrawingPanel()
        {
            InitializeComponent();
        }

        private void DrawingPanel_Load(object sender, EventArgs e)
        {

        }

        private void DrawingPanel_Paint(object sender, PaintEventArgs e)
        {
            graphicsObject = this.CreateGraphics();
        }

        private void DrawingPanel_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {

            }
        }

        void createBasicWall(int wall_index, Color wall_colour, int x, int y, int width, int height, bool select)
        {
            //Create Basic Wall at MousePosition
            wallObject tempWall = new wallObject();
            tempWall.wall_index = wall_index;
            tempWall.wall_brush = new SolidBrush(wall_colour);
            tempWall.wall_rectangle = new Rectangle(x, y, width, height);
            wallArray.Add(tempWall);
            String tempStr = "wall:" + tempWall.wall_index + ":" + (wallArray.Count - 1) + ":" + tempWall.wall_rectangle.X.ToString() + ":" + tempWall.wall_rectangle.Y.ToString();
            selectToolStripMenuItem.DropDownItems.Add(tempStr);
            if (select == true)
            {
                selectedType = "wall";
                if (wallArray.Count == 1)
                {
                    selectToolStripMenuItem.DropDownItems[0].Font = new Font(selectToolStripMenuItem.DropDownItems[0].Font.Name, selectToolStripMenuItem.DropDownItems[0].Font.Size, FontStyle.Bold);
                    Text = "C# Todesesser Map Editor - " +
                    "wall:" +
                    tempWall.wall_index +
                    ":" +
                    (wallArray.Count - 1) +
                    ":" +
                    tempWall.wall_rectangle.X.ToString() +
                    ":" +
                    tempWall.wall_rectangle.Y.ToString();

                }
                //Select Item
                selectedWall = wallArray.Count - 1;
                foreach (ToolStripItem item in selectToolStripMenuItem.DropDownItems)
                {
                    item.Font = new Font(item.Font.Name, item.Font.Size, FontStyle.Regular);
                }
                selectToolStripMenuItem.DropDownItems[selectedWall].Font = new Font(selectToolStripMenuItem.Font.Name, selectToolStripMenuItem.Font.Size, FontStyle.Bold);
            }
        }

        private void basicWallToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Create Basic Wall at MousePosition
            createBasicWall(1, Color.Red, mouseX, mouseY, 250, 250, true);
        }

        private void DrawingPanel_MouseMove(object sender, MouseEventArgs e)
        {
            mouseX = e.X;
            mouseY = e.Y;
        }
        public Boolean hitTestMouse(Rectangle hitObject, int MouseX, int MouseY)
        {
            if (MouseX > hitObject.X && MouseY > hitObject.Y)
            {
                if (MouseX < (hitObject.X + hitObject.Width) && MouseY < (hitObject.Y + hitObject.Height))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        private void DrawingPanel_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                spawnableMenu.Show(new Point(MousePosition.X, MousePosition.Y));
            }
        }

        private void DrawingPanel_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                loopMovement = true;
            }
        }

        private void loop_Tick(object sender, EventArgs e)
        {
            loop.Interval = redrawRate;
            graphicsObject.Clear(Color.White);
            foreach (wallObject wall in wallArray)
            {
                graphicsObject.FillRectangle(wall.wall_brush, wall.wall_rectangle);
            }
            foreach (objectObject objectO in objectArray)
            {
                graphicsObject.DrawString(objectO.text, objectO.font, objectO.object_brush, new PointF(objectO.object_rectangle.X, objectO.object_rectangle.Y));
            }
            Update();
            //Resize Wall
            if (loopMovement == true)
            {
                if (sizeState != "null")
                {
                    //Bottom
                    if (sizeState == "B")
                    {
                        int tempHeight = mouseY - sizeWall.wall_rectangle.Y;
                        if (tempHeight >= 10)
                        {
                            sizeWall.wall_rectangle.Height = tempHeight;
                        }
                        else
                        {
                            sizeWall.wall_rectangle.Height = 10;
                        }
                    }
                    //Top
                    if (sizeState == "T")
                    {
                        if (mouseY >= 0)
                        {
                            int change = sizeWall.wall_rectangle.Y - mouseY;
                            int newHeight = sizeWall.wall_rectangle.Height + change;
                            int newY = sizeWall.wall_rectangle.Y - change;
                            sizeWall.wall_rectangle.Height = newHeight;
                            sizeWall.wall_rectangle.Y = newY;
                        }
                        else
                        {
                            int change = sizeWall.wall_rectangle.Y - 0;
                            int newHeight = sizeWall.wall_rectangle.Height + change;
                            int newY = sizeWall.wall_rectangle.Y - change;
                            sizeWall.wall_rectangle.Height = newHeight;
                            sizeWall.wall_rectangle.Y = newY;
                        }
                    }
                    //Right
                    if (sizeState == "R")
                    {
                        int tempWidth = mouseX - sizeWall.wall_rectangle.X;
                        if (tempWidth >= 10)
                        {
                            sizeWall.wall_rectangle.Width = tempWidth;
                        }
                        else
                        {
                            sizeWall.wall_rectangle.Width = 10;
                        }
                    }
                    //Movement
                    if (sizeState == "M")
                    {
                        int wmouseX = mouseX - sizeWall.wall_rectangle.X;
                        int wmouseY = mouseY - sizeWall.wall_rectangle.Y;
                        if (tempwmousex == 0 && tempwmousey == 0)
                        {
                            tempwmousex = wmouseX;
                            tempwmousey = wmouseY;
                        }
                        int changeX = (sizeWall.wall_rectangle.X + (tempwmousex)) - mouseX;
                        int changeY = (sizeWall.wall_rectangle.Y + (tempwmousey)) - mouseY;
                        sizeWall.wall_rectangle.X = sizeWall.wall_rectangle.X - changeX;
                        sizeWall.wall_rectangle.Y = sizeWall.wall_rectangle.Y - changeY;
                    }
                    //Left
                    if (sizeState == "L")
                    {
                        if (mouseX >= 0)
                        {
                            int change = sizeWall.wall_rectangle.X - mouseX;
                            int newWidth = sizeWall.wall_rectangle.Width + change;
                            int newX = sizeWall.wall_rectangle.X - change;
                            sizeWall.wall_rectangle.Width = newWidth;
                            sizeWall.wall_rectangle.X = newX;
                        }
                        else
                        {
                            int change = sizeWall.wall_rectangle.X - 0;
                            int newWidth = sizeWall.wall_rectangle.Width + change;
                            int newX = sizeWall.wall_rectangle.X - change;
                            sizeWall.wall_rectangle.Width = newWidth;
                            sizeWall.wall_rectangle.X = newX;
                        }
                    }
                    //Bottom Left
                    if (sizeState == "BL")
                    {
                        if (mouseY >= 0)
                        {
                            int tempHeight = mouseY - sizeWall.wall_rectangle.Y;
                            if (tempHeight >= 10)
                            {
                                sizeWall.wall_rectangle.Height = tempHeight;
                            }
                            else
                            {
                                sizeWall.wall_rectangle.Height = 10;
                            }
                        }
                        if (mouseX >= 0)
                        {
                            int change = sizeWall.wall_rectangle.X - mouseX;
                            int newWidth = sizeWall.wall_rectangle.Width + change;
                            int newX = sizeWall.wall_rectangle.X - change;
                            sizeWall.wall_rectangle.Width = newWidth;
                            sizeWall.wall_rectangle.X = newX;
                        }
                        else
                        {
                            int change = sizeWall.wall_rectangle.X - 0;
                            int newWidth = sizeWall.wall_rectangle.Width + change;
                            int newX = sizeWall.wall_rectangle.X - change;
                            sizeWall.wall_rectangle.Width = newWidth;
                            sizeWall.wall_rectangle.X = newX;
                        }
                    }
                    //Top Right
                    if (sizeState == "TR")
                    {
                        if (mouseY >= 0)
                        {
                            int change = sizeWall.wall_rectangle.Y - mouseY;
                            int newHeight = sizeWall.wall_rectangle.Height + change;
                            int newY = sizeWall.wall_rectangle.Y - change;
                            sizeWall.wall_rectangle.Height = newHeight;
                            sizeWall.wall_rectangle.Y = newY;
                        }
                        else
                        {
                            int change = sizeWall.wall_rectangle.Y - 0;
                            int newHeight = sizeWall.wall_rectangle.Height + change;
                            int newY = sizeWall.wall_rectangle.Y - change;
                            sizeWall.wall_rectangle.Height = newHeight;
                            sizeWall.wall_rectangle.Y = newY;
                        }
                        if (mouseX >= 0)
                        {
                            int tempWidth = mouseX - sizeWall.wall_rectangle.X;
                            if (tempWidth >= 10)
                            {
                                sizeWall.wall_rectangle.Width = tempWidth;
                            }
                            else
                            {
                                sizeWall.wall_rectangle.Width = 10;
                            }
                        }
                    }
                    //Top Left
                    if (sizeState == "TL")
                    {
                        if (mouseX >= 0)
                        {
                            int change = sizeWall.wall_rectangle.X - mouseX;
                            int newWidth = sizeWall.wall_rectangle.Width + change;
                            int newX = sizeWall.wall_rectangle.X - change;
                            sizeWall.wall_rectangle.Width = newWidth;
                            sizeWall.wall_rectangle.X = newX;
                        }
                        else
                        {
                            int change = sizeWall.wall_rectangle.X - 0;
                            int newWidth = sizeWall.wall_rectangle.Width + change;
                            int newX = sizeWall.wall_rectangle.X - change;
                            sizeWall.wall_rectangle.Width = newWidth;
                            sizeWall.wall_rectangle.X = newX;
                        }
                        if (mouseY >= 0)
                        {
                            int change = sizeWall.wall_rectangle.Y - mouseY;
                            int newHeight = sizeWall.wall_rectangle.Height + change;
                            int newY = sizeWall.wall_rectangle.Y - change;
                            sizeWall.wall_rectangle.Height = newHeight;
                            sizeWall.wall_rectangle.Y = newY;
                        }
                        else
                        {
                            int change = sizeWall.wall_rectangle.Y - 0;
                            int newHeight = sizeWall.wall_rectangle.Height + change;
                            int newY = sizeWall.wall_rectangle.Y - change;
                            sizeWall.wall_rectangle.Height = newHeight;
                            sizeWall.wall_rectangle.Y = newY;
                        }
                    }
                    //Bottom Right
                    if (sizeState == "BR")
                    {
                        if (mouseY >= 0)
                        {
                            int tempHeight = mouseY - sizeWall.wall_rectangle.Y;
                            if (tempHeight >= 10)
                            {
                                sizeWall.wall_rectangle.Height = tempHeight;
                            }
                            else
                            {
                                sizeWall.wall_rectangle.Height = 10;
                            }
                        }
                        if (mouseX >= 0)
                        {
                            int tempWidth = mouseX - sizeWall.wall_rectangle.X;
                            if (tempWidth >= 10)
                            {
                                sizeWall.wall_rectangle.Width = tempWidth;
                            }
                            else
                            {
                                sizeWall.wall_rectangle.Width = 10;
                            }
                        }
                    }
                }
            }
            //If we can resize wall.
            if (loopMovement == false)
            {
                wallObject wall;
                if (wallArray.Count >= 1)
                {
                    wall = (wallObject)wallArray[selectedWall];
                    if (mouseX >= wall.wall_rectangle.X &&
                        mouseX <= wall.wall_rectangle.X + 5 &&
                        mouseY >= wall.wall_rectangle.Y &&
                        mouseY <= wall.wall_rectangle.Y + 5)
                    {
                        //Top Left
                        Cursor = Cursors.SizeNWSE;
                        //Set State
                        sizeState = "TL";
                        sizeWall = wall;
                    }
                    else if (mouseX >= (wall.wall_rectangle.X + wall.wall_rectangle.Width) &&
                        mouseX <= (wall.wall_rectangle.X + wall.wall_rectangle.Width) + 5 &&
                        mouseY >= wall.wall_rectangle.Y &&
                        mouseY <= wall.wall_rectangle.Y + 5)
                    {
                        //Top Right
                        Cursor = Cursors.SizeNESW;
                        //Set State
                        sizeState = "TR";
                        sizeWall = wall;
                    }
                    else if (mouseX >= wall.wall_rectangle.X - 5 &&
                        mouseX <= wall.wall_rectangle.X &&
                        mouseY >= (wall.wall_rectangle.Y + wall.wall_rectangle.Height) - 5 &&
                        mouseY <= (wall.wall_rectangle.Y + wall.wall_rectangle.Height) )
                    {
                        //Bottom Left
                        Cursor = Cursors.SizeNESW;
                        //Set State
                        sizeState = "BL";
                        sizeWall = wall;
                    }
                    else if (mouseX >= (wall.wall_rectangle.X + wall.wall_rectangle.Width) &&
                        mouseX <= (wall.wall_rectangle.X + wall.wall_rectangle.Width) + 5 &&
                        mouseY >= (wall.wall_rectangle.Y + wall.wall_rectangle.Height) &&
                        mouseY <= (wall.wall_rectangle.Y + wall.wall_rectangle.Height) + 5)
                    {
                        //Bottom Right
                        Cursor = Cursors.SizeNWSE;
                        //Set State
                        sizeState = "BR";
                        sizeWall = wall;
                    }
                    else if (mouseY >= wall.wall_rectangle.Y && 
                             mouseY <= wall.wall_rectangle.Y + 5 &&
                             mouseX >= wall.wall_rectangle.X &&
                             mouseX <= (wall.wall_rectangle.X + wall.wall_rectangle.Width))
                    {
                        //Top
                        Cursor = Cursors.SizeNS;
                        //Set State
                        sizeState = "T";
                        sizeWall = wall;
                    }
                    else if (mouseY >= (wall.wall_rectangle.Y + wall.wall_rectangle.Height) - 5 && 
                             mouseY <= (wall.wall_rectangle.Y + wall.wall_rectangle.Height) &&
                             mouseX >= (wall.wall_rectangle.X) &&
                             mouseX <= (wall.wall_rectangle.X + wall.wall_rectangle.Width))
                    {
                        //Bottom
                        Cursor = Cursors.SizeNS;
                        //Set State
                        sizeState = "B";
                        sizeWall = wall;
                    }
                    else if (mouseX >= wall.wall_rectangle.X && 
                             mouseX <= wall.wall_rectangle.X + 5 &&
                             mouseY >= wall.wall_rectangle.Y &&
                             mouseY <= (wall.wall_rectangle.Y + wall.wall_rectangle.Height))
                    {
                        //Left
                        Cursor = Cursors.SizeWE;
                        sizeState = "L";
                        sizeWall = wall;
                    }
                    else if (mouseX >= (wall.wall_rectangle.X + wall.wall_rectangle.Width) - 5 && 
                             mouseX <= (wall.wall_rectangle.X + wall.wall_rectangle.Width) &&
                             mouseY >= (wall.wall_rectangle.Y) &&
                             mouseY <= (wall.wall_rectangle.Y + wall.wall_rectangle.Height))
                    {
                        //Right
                        Cursor = Cursors.SizeWE;
                        sizeState = "R";
                        sizeWall = wall;
                    }
                    else if (mouseX >= wall.wall_rectangle.X &&
                            mouseX <= (wall.wall_rectangle.X + wall.wall_rectangle.Width) &&
                            mouseY >= wall.wall_rectangle.Y &&
                            mouseY <= (wall.wall_rectangle.Y + wall.wall_rectangle.Height))
                    {
                        //Movement
                        Cursor = Cursors.SizeAll;
                        sizeState = "M";
                        sizeWall = wall;
                    }
                    else
                    {
                        Cursor = Cursors.Default;
                        sizeWall = null;
                        sizeState = "null";
                    }
                }
            }
        }

        private void DrawingPanel_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                loopMovement = false;
                tempwmousex = 0;
                tempwmousey = 0;
            }
        }

        private void DrawingPanel_KeyPress(object sender, KeyPressEventArgs e)
        {
            
        }

        private void DrawingPanel_KeyDown(object sender, KeyEventArgs e)
        {
            if (selectedType == "wall")
            {
                try
                {
                    wallObject tempWall = (wallObject)wallArray[selectedWall];
                    if (e.KeyValue == 38)
                    {
                        if (e.Control == false)
                        {
                            tempWall.wall_rectangle.Y -= 1;
                        }
                        else
                        {
                            tempWall.wall_rectangle.Y -= 8;
                        }
                    }
                    if (e.KeyValue == 40)
                    {
                        if (e.Control == false)
                        {
                            tempWall.wall_rectangle.Y += 1;
                        }
                        else
                        {
                            tempWall.wall_rectangle.Y += 8;
                        }
                    }
                    if (e.KeyValue == 37)
                    {
                        if (e.Control == false)
                        {
                            tempWall.wall_rectangle.X -= 1;
                        }
                        else
                        {
                            tempWall.wall_rectangle.X -= 8;
                        }
                    }
                    if (e.KeyValue == 39)
                    {
                        if (e.Control == false)
                        {
                            tempWall.wall_rectangle.X += 1;
                        }
                        else
                        {
                            tempWall.wall_rectangle.X += 8;
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            if (selectedType == "object")
            {
                try
                {
                    objectObject tempWall = (objectObject)objectArray[selectedWall];
                    if (e.KeyValue == 38)
                    {
                        if (e.Control == false)
                        {
                            tempWall.object_rectangle.Y -= 1;
                        }
                        else
                        {
                            tempWall.object_rectangle.Y -= 8;
                        }
                    }
                    if (e.KeyValue == 40)
                    {
                        if (e.Control == false)
                        {
                            tempWall.object_rectangle.Y += 1;
                        }
                        else
                        {
                            tempWall.object_rectangle.Y += 8;
                        }
                    }
                    if (e.KeyValue == 37)
                    {
                        if (e.Control == false)
                        {
                            tempWall.object_rectangle.X -= 1;
                        }
                        else
                        {
                            tempWall.object_rectangle.X -= 8;
                        }
                    }
                    if (e.KeyValue == 39)
                    {
                        if (e.Control == false)
                        {
                            tempWall.object_rectangle.X += 1;
                        }
                        else
                        {
                            tempWall.object_rectangle.X += 8;
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void selectToolStripMenuItem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            foreach (ToolStripItem item in selectToolStripMenuItem.DropDownItems)
            {
                item.Font = new Font(item.Font.Name, item.Font.Size, FontStyle.Regular);
            }
            foreach (ToolStripItem item in selectObjectToolStripMenuItem.DropDownItems)
            {
                item.Font = new Font(item.Font.Name, item.Font.Size, FontStyle.Regular);
            }
            e.ClickedItem.Font = new Font(e.ClickedItem.Font.Name, e.ClickedItem.Font.Size, FontStyle.Bold);
            string[] itemParseData;
            itemParseData = Regex.Split(e.ClickedItem.Text, ":");
            selectedWall = int.Parse(itemParseData[2]);
            wallObject tempWall = (wallObject)wallArray[selectedWall];
            Text = "C# Todesesser Map Editor - " +
                "wall:" +
                tempWall.wall_index +
                ":" +
                itemParseData[2].ToString() +
                ":" +
                tempWall.wall_rectangle.X.ToString() +
                ":" +
                tempWall.wall_rectangle.Y.ToString();
            selectedType = "wall";
        }

        private void editSelectedObjectWallToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (selectedType == "wall")
            {
                EditWall tempEditDialog = new EditWall((wallObject)wallArray[selectedWall]);
                tempEditDialog.ShowDialog();
            }
            if (selectedType == "object")
            {
                EditObject tempEditDialog = new EditObject((objectObject)objectArray[selectedWall]);
                tempEditDialog.ShowDialog();
            }
        }

        private void deleteSelectedObjectWallToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (selectedType == "wall")
            {
                MessageBox.Show("Not Implemented Yet!");
            }
            if (selectedType == "object")
            {
                MessageBox.Show("Not Implemented Yet!");
            }
        }

        private void newMapToolStripMenuItem_Click(object sender, EventArgs e)
        {
            wallArray.Clear();
            Text = "C# Todesesser Map Editor";
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void saveMapToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog tempSfileDialog = new SaveFileDialog();
            tempSfileDialog.Filter = "Todesesser Walls file (*.ini)|*.ini";
            if (tempSfileDialog.ShowDialog() == DialogResult.OK)
            {
                if (File.Exists(tempSfileDialog.FileName) == true)
                {
                    File.Delete(tempSfileDialog.FileName);
                }
                File.AppendAllText(tempSfileDialog.FileName, "[walls]" + Environment.NewLine);
                int forcount = 1;
                foreach (wallObject wall in wallArray)
                {
                    string HexColour = string.Format("0x{0:X8}", wall.wall_brush.Color.ToArgb());
                    HexColour = HexColour.Substring(HexColour.Length - 6, 6);
                    HexColour = "{0x" + HexColour + "}";
                    String lineToWrite;
                    if (forcount != wallArray.Count)
                    {
                        lineToWrite = wall.wall_index.ToString() + ":" +
                                             wall.wall_rectangle.X.ToString() + ":" +
                                             wall.wall_rectangle.Y.ToString() + ":" +
                                             wall.wall_rectangle.Width.ToString() + ":" +
                                             wall.wall_rectangle.Height.ToString() + ":" +
                                             HexColour + ";" + Environment.NewLine;
                    }
                    else
                    {
                        lineToWrite = wall.wall_index.ToString() + ":" +
                                             wall.wall_rectangle.X.ToString() + ":" +
                                             wall.wall_rectangle.Y.ToString() + ":" +
                                             wall.wall_rectangle.Width.ToString() + ":" +
                                             wall.wall_rectangle.Height.ToString() + ":" +
                                             HexColour + ";";
                    }
                    File.AppendAllText(tempSfileDialog.FileName, lineToWrite);
                    forcount += 1;
                }
            }
            SaveFileDialog tempSfileDialog2 = new SaveFileDialog();
            tempSfileDialog2.Filter = "Todesesser Objects file (*.ini)|*.ini";
            if (tempSfileDialog2.ShowDialog() == DialogResult.OK)
            {
                if (File.Exists(tempSfileDialog2.FileName) == true)
                {
                    File.Delete(tempSfileDialog2.FileName);
                }
                File.AppendAllText(tempSfileDialog2.FileName, "[objects]" + Environment.NewLine);
                foreach (objectObject objectO in objectArray)
                {
                    String lineToWrite = objectO.object_index.ToString() + ":" +
                                         objectO.object_rectangle.X.ToString() + ":" +
                                         objectO.object_rectangle.Y.ToString() + ":" +
                                         objectO.object_rectangle.Width.ToString() + ":" +
                                         objectO.object_rectangle.Height.ToString() + ":" +
                                         Environment.NewLine;
                    File.AppendAllText(tempSfileDialog2.FileName, lineToWrite);
                }
            }
        }

        private void openMapToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog tempOfileDialog = new OpenFileDialog();
            tempOfileDialog.Filter = "Todesesser Walls file (*.ini)|*.ini";
            if (tempOfileDialog.ShowDialog() == DialogResult.OK)
            {
                string[] wallFileLines = File.ReadAllLines(tempOfileDialog.FileName);
                foreach (string line in wallFileLines)
                {
                    if (line.Contains("[walls]") == false)
                    {
                        string cleanedLine = line.Replace(";", "");
                        string[] splitLine = Regex.Split(cleanedLine, ":");
                        string cleanedColour = splitLine[5].Replace("{", "").Replace("}", "").Replace("0x", "");
                        int hexInt = int.Parse(cleanedColour, System.Globalization.NumberStyles.AllowHexSpecifier);
                        Color tempColour = Color.FromArgb(hexInt);
                        Color realColour = new Color();
                        realColour = Color.FromArgb(255,tempColour.R, tempColour.G, tempColour.B);
                        createBasicWall(int.Parse(splitLine[0]), realColour, int.Parse(splitLine[1]), int.Parse(splitLine[2]), int.Parse(splitLine[3]), int.Parse(splitLine[4]), false);
                        MessageBox.Show("Added Wall!");
                    }
                }
            }
            OpenFileDialog tempOfileDialog2 = new OpenFileDialog();
            tempOfileDialog2.Filter = "Todesesser Objects file (*.ini)|*.ini";
            if (tempOfileDialog2.ShowDialog() == DialogResult.OK)
            {
                string[] wallFileLines2 = File.ReadAllLines(tempOfileDialog2.FileName);
                foreach (string line in wallFileLines2)
                {
                    if (line.Contains("[objects]") == false)
                    {
                        string cleanedLine = line.Replace(";", "");
                        string[] splitLine = Regex.Split(cleanedLine, ":");
                        createBasicObject(int.Parse(splitLine[0]),"ER", int.Parse(splitLine[1]), int.Parse(splitLine[2]), int.Parse(splitLine[3]), int.Parse(splitLine[4]), false, Color.Green);
                    }
                }
            }
        }

        void createBasicObject(int object_index, string object_string, int x, int y, int width, int height, bool select, Color object_colour)
        {
            //graphicsObject.DrawString("ES", Font, tempObject.object_brush, new PointF(x, y));
            //Create Basic Wall at MousePosition
            objectObject tempObject = new objectObject();
            tempObject.object_index = object_index;
            tempObject.object_brush = new SolidBrush(object_colour);
            tempObject.object_rectangle = new Rectangle(x, y, width, height);
            tempObject.text = object_string;
            tempObject.font = Font;
            objectArray.Add(tempObject);

            //Add to DropDown Menu
            String tempStr = "object:" + tempObject.object_index + ":" + (objectArray.Count - 1) + ":" + tempObject.object_rectangle.X.ToString() + ":" + tempObject.object_rectangle.Y.ToString();
            selectObjectToolStripMenuItem.DropDownItems.Add(tempStr);
            //Select the object if the bool select = true.
            if (select == true)
            {
                selectedType = "object";
                if (wallArray.Count == 1)
                {
                    selectObjectToolStripMenuItem.DropDownItems[0].Font = new Font(selectObjectToolStripMenuItem.DropDownItems[0].Font.Name, selectObjectToolStripMenuItem.DropDownItems[0].Font.Size, FontStyle.Bold);
                    Text = "C# Todesesser Map Editor - " +
                    "wall:" +
                    tempObject.object_index +
                    ":" +
                    (wallArray.Count - 1) +
                    ":" +
                    tempObject.object_rectangle.X.ToString() +
                    ":" +
                    tempObject.object_rectangle.Y.ToString();

                }
                //Select Item
                selectedWall = wallArray.Count - 1;
                foreach (ToolStripItem item in selectObjectToolStripMenuItem.DropDownItems)
                {
                    item.Font = new Font(item.Font.Name, item.Font.Size, FontStyle.Regular);
                }
                selectObjectToolStripMenuItem.DropDownItems[selectedWall].Font = new Font(selectObjectToolStripMenuItem.Font.Name, selectObjectToolStripMenuItem.Font.Size, FontStyle.Bold);
            }
        }

        private void enemySpawnerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            createBasicObject(1, "ES", mouseX, mouseY, 25, 25, false, Color.Green);
        }

        private void selectObjectToolStripMenuItem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            foreach (ToolStripItem item in selectToolStripMenuItem.DropDownItems)
            {
                item.Font = new Font(item.Font.Name, item.Font.Size, FontStyle.Regular);
            }
            foreach (ToolStripItem item in selectObjectToolStripMenuItem.DropDownItems)
            {
                item.Font = new Font(item.Font.Name, item.Font.Size, FontStyle.Regular);
            }
            e.ClickedItem.Font = new Font(e.ClickedItem.Font.Name, e.ClickedItem.Font.Size, FontStyle.Bold);
            string[] itemParseData;
            itemParseData = Regex.Split(e.ClickedItem.Text, ":");
            selectedWall = int.Parse(itemParseData[2]);
            objectObject tempObject = (objectObject)objectArray[selectedWall];
            Text = "C# Todesesser Map Editor - " +
                "object:" +
                tempObject.object_index +
                ":" +
                itemParseData[2].ToString() +
                ":" +
                tempObject.object_rectangle.X.ToString() +
                ":" +
                tempObject.object_rectangle.Y.ToString();
            selectedType = "object";
        }

        private void playerSpawnPointToolStripMenuItem_Click(object sender, EventArgs e)
        {
            createBasicObject(2, "PS", mouseX, mouseY, 25, 25, false, Color.Red);
        }

        private void mapSettingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            EditMapSettings tempEditMapSettingsDialog = new EditMapSettings();
            tempEditMapSettingsDialog.ShowDialog();
        }

        private void mapEditorSettingsToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            MapEditorSettings tempMapEditorSettings = new MapEditorSettings(redrawRate);
            if (tempMapEditorSettings.ShowDialog() == DialogResult.OK)
            {
                redrawRate = tempMapEditorSettings.newRedraw;
            }
        }

        private void barrierToolStripMenuItem_Click(object sender, EventArgs e)
        {
            createBasicWall(2, Color.LightGray, mouseX, mouseY, 200, 400, true);
        }
    }
}
